package gui;

/**
 * @author Ramy Refky 
 * */
public class SplitterComponentGraphics extends ComponentGraphics {

	private static final long serialVersionUID = 1L;
	
	int splitCount;
	int option;
	
	/**
	 * Constructor initializes SPLITTER component by initializing ComponentGraphics, and then painting the SplitterComponentLabel.
	 * @param MainGui, XLocation, YLocation, Width, Height, NumberOfInputs, splitCount, option
	 * */
	public SplitterComponentGraphics(MainGui mg,int originalXLoc,int originalYLoc,
			int originalWidth,int originalHeight,int numOfInputs,int splitCount,int option){
		super(mg,originalXLoc,originalYLoc,originalWidth,originalHeight,numOfInputs,numOfInputs);
		
		this.splitCount=splitCount;
		this.option=option;
		
		setSplitInputPins();
		setSplitOutputPins();
		
		this.remove(compLabel);
		compLabel=new SplitterComponentLabel(newXLoc,newYLoc,originalWidth,originalHeight);
		this.add(compLabel);
		setComponentZOrder(compLabel,0);
		repaint();
	}
	/**
	 * Gets the total number of grouped wires from the top right group.
	 * @return int
	 * */
	public int getSplitCount(){
		return splitCount;
	}
	/**
	 * Gets the option or type of this Splitter component. If option =1 draw
	 * component with top right group and input group. If option =2 draw
	 * component with bottom right group only. Normally a displayed splitter
	 * component is actually two overlapping components with different options.
	 * @return int
	 * */
	public int getOption(){
		return option;
	}
	@Override
	public void setOutputPins(int num){
	//do nothing
	}
	@Override
	public void setInputPins(int num){
	//do nothing	
	}
	@Override
	public int getNumberOfOutputs(){
		return getSplitCount();
	}
	
	@Override
	public void adjustPins(){
		
		if(option==1){
			int inputCount=getNumberOfInputs();
			
			if(inputCount<=1){
				if(inputCount%2==1){
					for(int i=1;i<=inputCount;i++){
						int h = i*newHeight/(inputCount+1)-1;
						inputPins.get(i-1).zoomAdjust(0,h,newWidth/2,h);
					}
				}
				else{
					for(int i=1;i<=inputCount;i++){
						int h = (2*(i-1)+1)*newHeight/(inputCount*2) -1;
						inputPins.get(i-1).zoomAdjust(0,h,newWidth/2,h);
					}
				}
			}
			else{
				for(int i=1;i<=inputCount;i++){
					int h = newHeight/(2) -1;
					inputPins.get(i-1).zoomAdjust(0,h,newWidth/2,h);
				}
			}
		}		
		if(option==1){
			for(int i=0;i<getSplitCount();i++){
				outputPins.get(i).zoomAdjust(newWidth*4/5,newHeight/3-2,newWidth,newHeight/3-2);
			}
		}
		if(option==2){
			for(int i=0;i<getSplitCount();i++){
				outputPins.get(i).zoomAdjust(newWidth*4/5,newHeight*2/3-1,newWidth,newHeight*2/3-1);
			}
		}
		repaint();
	}
	/**
	 * Initializes the input ComponentPins and positions them accordingly.
	 * */
	public void setSplitInputPins(){
		
		if(option==1){
			if(numOfInputs<=1){
				if(numOfInputs%2==1){
					for(int i=1;i<=numOfInputs;i++){
						int h = i*originalHeight/(numOfInputs+1)-1;
						ComponentPin pin = new ComponentPin(mg,this,0,h,originalWidth/2,h);
						inputPins.add(pin);
						this.add(inputPins.get(inputPins.size()-1));
					}
				}
				else{
					for(int i=1;i<=numOfInputs;i++){
						int h = (2*(i-1)+1)*originalHeight/(numOfInputs*2) -1;
						ComponentPin pin = new ComponentPin(mg,this,0,h,originalWidth/2,h);
						inputPins.add(pin);
						this.add(inputPins.get(inputPins.size()-1));
					}
				}
			}
			else{
				for(int i=1;i<=numOfInputs;i++){
					int h = originalHeight/(2) -1;
					ComponentPin pin = new ComponentPin(mg,this,0,h,originalWidth/2,h);
					pin.setAsGrouped();
					inputPins.add(pin);
					this.add(inputPins.get(inputPins.size()-1));
				}
			}
			repaint();
		}
	}
	/**
	 * Initializes the output ComponentPins and positions them accordingly.
	 * */
	public void setSplitOutputPins(){
		if(option==1){
			
			for(int i=0;i<getSplitCount();i++){
				ComponentPin pin = new ComponentPin(mg,this,originalWidth*4/5,originalHeight/3-2,originalWidth,originalHeight/3-2);
				pin.setTypeAsOutput();
				if(getSplitCount()>1){
					pin.setAsGrouped();
				}
				outputPins.add(pin);
				this.add(outputPins.get(outputPins.size()-1));	
			}
		}
		if(option==2){
			
			for(int i=0; i<getSplitCount();i++){
				ComponentPin pin = new ComponentPin(mg,this,getWidth()*4/5,getHeight()*2/3-1,getWidth(),getHeight()*2/3-1);
				pin.setTypeAsOutput();
				if(splitCount>1){
					pin.setAsGrouped();
				}
				outputPins.add(pin);
				this.add(outputPins.get(outputPins.size()-1));
			}
		}
		repaint();
	}
}
